package com.topisv.tms.web.work.ExceptionOrder;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.ReturnExceptionInfo;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.service.ReturnExceptionInfoService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.work.WorkOrderService;
import com.topisv.tms.web.base.BaseDataTableController;

/**
 * 回传异常订单管理—control
 * @author ch
 *
 */
@Controller
@RequestMapping(value = "/v2/returnException")
public class ReturnExceptionOrderController extends BaseDataTableController {

	private static final Logger logger = LoggerFactory.getLogger(ReturnExceptionOrderController.class);
	
	@Autowired
	private ReturnExceptionInfoService reService;
	@Autowired
	private WorkOrderService workOrderService;
	
	@RequestMapping(value = "show")
	public String show(Model model){
		fillSelectValue(model, null);
		return "work/exceptionOrder/returnExceptionMgr";
	}
	
	@RequestMapping(value = "query")
	public void query(@RequestParam(value = "wtId") String wtId,
			HttpServletResponse response,Model model){
		
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			
			WorkOrder workOrder = workOrderService.findOne(Long.valueOf(wtId));
			ReturnExceptionInfo rei = reService.findByTaskCodeAndOrderNo(workOrder.getTaskCode(), workOrder.getOrder().getOrdeno());
			
			
			json.put("info", rei);
			json.put("content", "查询成功");
			json.put("status", "1");
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content","服务器异常");
			out.print(json.toString());
			log.error("回传异常订单备注异常",e);
		}
	}
	
	@RequestMapping(value = "addRemark")
	public void addRemark(
			@RequestParam(value = "djId",required=false) String djId,
			@RequestParam(value = "wtId") String wtId,
			@RequestParam(value = "failType") String failType,
			@RequestParam(value = "failRemark") String failRemark,
			HttpServletResponse response,Model model) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			
			ShiroUser su = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			if(StringUtils.isNotEmpty(djId)){
				ReturnExceptionInfo re = reService.findOne(Long.valueOf(djId));
				re.setFailType(failType);
				re.setFailRemark(failRemark);
				re.setRemarkBy(su.getJobuuid());
				re.setRemarkTime(new Date());
				reService.save(re);
			}else {
				WorkOrder workOrder = workOrderService.findOne(Long.valueOf(wtId));
				
				ReturnExceptionInfo re = new ReturnExceptionInfo();
				re.setBatchCode(workOrder.getBatchCode());
				re.setTaskCode(workOrder.getTaskCode());
				re.setOrderNo(workOrder.getOrder().getOrdeno());
				re.setFailType(failType);
				re.setFailRemark(failRemark);
				re.setRemarkBy(su.getJobuuid());
				re.setRemarkTime(new Date());
				reService.save(re);
			}
			json.put("content", "备注成功");
			json.put("status", "1");
			out.print(json.toString());
		} catch (Exception e) {e.printStackTrace();
			json.put("status", "0");
			json.put("content","服务器异常");
			out.print(json.toString());
			log.error("回传异常订单备注异常",e);
		}
	}
	
	@RequestMapping(value = "toSendFinish")
	public void toSendFinish(
			@RequestParam(value = "djId") String djId,
			HttpServletResponse response,Model model) {
		JSONObject json = null;
		PrintWriter out = null;
		try {
			json = new JSONObject();
			out = response.getWriter();
			
			ReturnExceptionInfo rei = reService.findOne(Long.valueOf(djId));
			reService.finish(rei);
			
			json.put("content", "修改成功");
			json.put("status", "1");
			out.print(json.toString());
		} catch (Exception e) {
			json.put("status", "0");
			json.put("content","服务器异常");
			out.print(json.toString());
			log.error("回传异常订单备注异常",e);
		}
	}
	
}
